package com.heima.lock.sjms.单例模式.最优单例05;

import com.heima.lock.sjms.单例模式.双检锁03.Student;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author 陈辉
 * @data 2024 11:28
 *
 * 枚举：天生单例, 枚举中的枚举项是枚举类的对象，每个对象都是单例。
 */
public enum Season {
    SPRING
}

class Demo{
    public static void main(String[] args) throws Exception {
        Season spring1 = Season.SPRING;
        System.out.println("spring1 = " + spring1);


        //枚举类不支持反射创建对象
        /*Class clazz = spring1.getClass();
        Season spring2 = (Season) clazz.newInstance();
        System.out.println("spring2 = " + spring2);*/


        //对stu1进行序列化操作
        ObjectOutputStream oos  = new ObjectOutputStream(new FileOutputStream("D:\\a.txt"));
        oos.writeObject(spring1);
        oos.close();

        //对stu1进行反序列化操作
        ObjectInputStream ois  = new ObjectInputStream(new FileInputStream("D:\\a.txt"));
        Season spring3 = (Season) ois.readObject();
        System.out.println("spring3 = " + spring3);
        ois.close();
    }
}
